﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAQ;
using System.Threading;

namespace DAQ.Common
{
    public static class Config
    {
        static readonly AutoResetEvent autoResetEvent = new AutoResetEvent(true);
         static Config()
        {
            List<CfgItem> list ;
            string path = System.IO.Path.GetDirectoryName(App.ConfigFileName);
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            if (!System.IO.File.Exists(App.ConfigFileName))
            {
                list = new List<CfgItem>();
                list.SerializeToXml(App.ConfigFileName);
                SaveConfig(CFGS.BTSR_COM,CFGS.PortName, "COM4");
                SaveConfig(CFGS.BTSR_COM,CFGS.BaudRate, "56700");
                SaveConfig(CFGS.MODBUS,CFGS.IP,"127.0.0.1");
                SaveConfig(CFGS.MATERIALS, CFGS.ASM, "BLACK BOBBIN ASM");
                SaveConfig(CFGS.MATERIALS, CFGS.WIRE, "copper wire");
                SaveConfig(CFGS.MATERIALS, CFGS.NO, "MATHINE 1");
                SaveConfig(CFGS.TENSIONS, CFGS.MAINTENSION_P1, "200");
                SaveConfig(CFGS.TENSIONS, CFGS.MAINTENSION_P2, "300");
                SaveConfig(CFGS.TENSIONS, CFGS.MAINTENSION_P3, "550");
                SaveConfig(CFGS.TENSIONS, CFGS.MAINTENSION_P4, "600");
                SaveConfig(CFGS.TENSIONS, CFGS.TENSION_ERROR, "100");
            }
        }
        public static List<CfgItem> GetAll()
        {
            autoResetEvent.WaitOne();
            var  list = ConfigEx.DeserializeFromXml<List<CfgItem>>(App.ConfigFileName);
            autoResetEvent.Set();
            return list;
        }
        public static void SaveConfig(string Section,string Key,string Value)
        {
            autoResetEvent.WaitOne();
            List<CfgItem> list= new List<CfgItem>();
            if(System.IO.File.Exists(App.ConfigFileName))
            {
                list = ConfigEx.DeserializeFromXml<List<CfgItem>>(App.ConfigFileName);
            }
            var quary = list.Where(x => x.Section == Section).Where(x => x.Key == Key);
            if(quary.Count()>0)
            {
                foreach( var a in quary)
                {
                    a.Value = Value;
                }
            }
            else
            {
                list.Add(new CfgItem { Section = Section, Key = Key, Value = Value });
            }
            list.SerializeToXml(App.ConfigFileName);
            autoResetEvent.Set();
        }
        public static string GetConfig(string Section,string Key)

        {
            autoResetEvent.WaitOne();
            string value = "";
            var list = ConfigEx.DeserializeFromXml<List<CfgItem>>(App.ConfigFileName);
            var quary = list.Where(x => x.Section == Section).Where(x => x.Key == Key);
            if (quary.Count() > 0)
                value= quary.First().Value;
            autoResetEvent.Set();
            return value;
        }
        [Serializable]
        public class CfgItem
        {
            public string Section { get; set; }
            public string Key { get; set; }
            public string Value { get; set; }
        }
    }
    public class CFGS
    {

        #region Sections
        public const string BTSR_COM = "BTSR_COM";
        public const string BTSR_DEV = "BTSR_DEV";
        public const string MODBUS = "MOBUS";
        public const string MATERIALS = "MATERIALS";
        public const string TENSIONS = "TENSIONS";
       
        #endregion

        #region Keys
        public const string BaudRate = "BaudRate";
        public const string PortName = "PortName";
        public const string IP = "IP";
        public const string WIRE = "WIRE";
        public const string ASM = "ASM";
        public const string NO = "NO";
        public const string MAINTENSION_P1 = "MAINTENSION_P1";
        public const string MAINTENSION_P2 = "MAINTENSION_P2";
        public const string MAINTENSION_P3 = "MAINTENSION_P3";
        public const string MAINTENSION_P4 = "MAINTENSION_P4";
        public const string TENSION_ERROR = "TENSION_ERROR";
        #endregion
    }

}
